package Test;

import Ctr.CustomerCtr;
import DB.CustomerDB;
import Model.Customer;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class CustomerCtrTest {
    @Test
    public void testInsertSupplier() throws Exception {

        CustomerCtr customerCtr = new CustomerCtr();
        customerCtr.insertCustomer(500, "Random Ass", "Mega Random", 9760, 123123123);

        if (customerCtr.findCustomer(500).getId() == 500) {
            System.out.println("Insert successful");
        }
        else {
            fail("Insert not successful");
        }
    }

    @Test
    public void testFindSupplier() throws Exception {
        CustomerCtr customerCtr = new CustomerCtr();

        int find = 3;

        Customer customer = customerCtr.findCustomer(find);

        if (customer != null) {
            System.out.println(
                    customer.getId() + " " +
                            customer.getName() + " " +
                            customer.getAddress() + " " +
                            customer.getZipCode() + " " +
                            customer.getCity() + " " +
                            customer.getPhoneNo());
        }
        else {
            fail("Error finding customer - customer is null");
        }
    }

    @Test
    public void testGetAllCustomers() throws Exception {

        CustomerCtr customerCtr = new CustomerCtr();
        ArrayList<Customer> customerList = customerCtr.getAllCustomers(false);

        if (customerList != null) {
            for (Customer c : customerList) {
                System.out.println(c.getName());
            }
        }
        else {
            fail("No customers found");
        }

    }
    // not usefull
    @Test
    public void testUpdateCustomer() throws Exception {
        CustomerCtr customerCtr = new CustomerCtr();
        String updateValue = "TestName";

        Customer customer = new Customer(4, "Test customer", "Sofiendalsvej 200", 9000 , "Aalborg", 88888888);

        customerCtr.updateCustomer(4);

        String customerName = customerCtr.findCustomer(4).getName();

        if (customerName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }
    }

    @Test
    public void testDeleteSupplier() throws Exception {
        CustomerDB customerDB = new CustomerDB();
        Customer customer = new Customer(10000, "Delete Me", "Delete Street", 9000 , "Delete City", 00000000);
        customerDB.insertCustomer(customer);

        if (customerDB.findCustomer(1000) != null) {

            customerDB.deleteCustomer(1000);

            if (customerDB.findCustomer(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
